package cn.ycc1.boot3qrcode.controller;

import cn.ycc1.boot3qrcode.service.QRCodeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ycc
 * @date 2023/11/21
 */
@Tag(name = "测试")
@Slf4j
@RestController
public class TestController {
    // https://www.cnblogs.com/lzp110119/p/17649636.html
    @Autowired
    private QRCodeService qrCodeService;

    @Operation(summary = "测试生成二维码")
    @GetMapping("/test")
    public String test() {

        return "hello test";
    }

    @GetMapping("qrCode")
    public void getQRCode(String codeContent, HttpServletResponse response) {
        try {
            qrCodeService.createQRCode2Stream(codeContent, response);
            log.info("成功生成二维码！");
        } catch (Exception e) {
            log.error("发生错误， 错误信息是：{}！", e.getMessage());
        }
    }

    @GetMapping("qrCodeFile")
    @Operation(summary = "生成二维码到文件")
    public void getQRCodeFile(String codeContent, String filePath) {
        try {
            // qrCodeService.createQRCode2Stream(codeContent, response);
            qrCodeService.createQRCode2File(codeContent, filePath);
            log.info("成功生成二维码！");
        } catch (Exception e) {
            log.error("发生错误， 错误信息是：{}！", e.getMessage());
        }
    }
}
